package com.zhz.lib.cms.manage.libAddrList;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.BeanUtils;

import com.zhz.lib.cms.domain.libaddrlist.libaddr;
import com.zhz.lib.cms.domain.type.Sortmap;
import com.zhz.lib.cms.manage.BaseActionWithSearch;
import com.zhz.lib.cms.utils.zhz.ZhzSecurityUtil;
import com.zhz.lib.cms.utils.zhz.ZhzSortMapUtil;
import com.zhz.lib.cms.vo.libaddr.LibaddrVO;
import com.zhz.project.common.util.alibaba.StringUtil;
import com.zhz.project.common.util.hibernate.BaseDAO;
import com.zhz.project.common.util.zhzutil.ZhzHttpRequestUtil;

public class libAddrAction extends BaseActionWithSearch {
    private static String TABLE      = "libaddr";
    private static String KEY        = "libaddrID";
    private static String LIST       = "libaddrlist";
    private static String ObjectName = "libaddr";

    //部门名称系列
    private Integer       departfatherid;

    /** 
     * 不给参数，默认执行此处
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    public ActionForward unspecified(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request, HttpServletResponse response)
                                                                                              throws Exception {
        return libaddrlist(mapping, form, request, response);
    }

    /**
     * 显示通讯录列表
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    @SuppressWarnings("unchecked")
    public ActionForward libaddrlist(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request, HttpServletResponse response) {

        // 获取部门
        String department = ZhzHttpRequestUtil.getParameters(request, "department");
        // 获取东西校区
        String area = ZhzHttpRequestUtil.getParameters(request, "area");

        request.setAttribute("department", department);

        //上面是提取参数并输出到页面中
        //////////////////////////////////////////////////////////////////////////
        String whereStatement = BaseDAO.WHERE;
        StringBuffer fanyeHref = new StringBuffer();
        fanyeHref.append("libAddrManage.do?method=libaddrlist");

        // 根据部门进行查询
        if (StringUtil.isNotBlank(department)) {
            whereStatement += "and department =" + department;
            fanyeHref.append("&department=");
            fanyeHref.append(department);
        }
        // 根据东西校区进行查询
        if (StringUtil.isNotBlank(area)) {
            if (StringUtil.equals(area, "west")) {
                whereStatement += "and  area='西校区'";
            } else if (StringUtil.equals(area, "east")) {
                whereStatement += "and  area='东校区'";
            }
            fanyeHref.append("&area=");
            fanyeHref.append(area);
            request.setAttribute("area", area);
        }

        List<libaddr> libaddrlist = getListWithFanyeHref(request, TABLE,
            whereStatement + BaseDAO.WHERE_DELETE, " order by " + KEY + " DESC", LIST, fanyeHref);

        List<LibaddrVO> libaddrVOlist = new ArrayList<LibaddrVO>();

        if (libaddrlist != null && libaddrlist.size() > 0) {
            for (libaddr libaddr : libaddrlist) {
                libaddrVOlist.add(changeToVo(libaddr));
            }
        }
        request.setAttribute(LIST, libaddrVOlist);

        return mapping.findForward("libaddr-list");

    }

    /**
     * 添加通讯录
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward libaddradd(ActionMapping mapping, ActionForm form,
                                    HttpServletRequest request, HttpServletResponse response) {

        request.setAttribute("departnamelist", ZhzSortMapUtil.getChildSortMapList(departfatherid,
            baseDao));

        return mapping.findForward("libaddr-add");

    }

    /**
     * 保存通讯录
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward libaddrsave(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request, HttpServletResponse response) {

        //姓名
        String name = ZhzHttpRequestUtil.getParameters(request, "name");
        //部门
        String department = ZhzHttpRequestUtil.getParameters(request, "department");
        //职位
        String position = ZhzHttpRequestUtil.getParameters(request, "position");
        //电话
        String tel = ZhzHttpRequestUtil.getParameters(request, "tel");
        //email
        String email = ZhzHttpRequestUtil.getParameters(request, "email");
        //校区
        String area = ZhzHttpRequestUtil.getParameters(request, "area");
        //备注
        String memo = ZhzHttpRequestUtil.getParameters(request, "memo");

        libaddr la = new libaddr();

        la.setName(name);
        la.setDepartment(department);
        la.setPosition(position);
        la.setTel(tel);
        la.setEmail(email);
        la.setArea(area);
        la.setMemo(memo);
        la.setEditor(ZhzSecurityUtil.getOperatorName());
        la.setEditorTime(new Date());
        la.setIfDelete(0);

        boolean save = baseDao.save(la);

        if (save) {
            request.setAttribute("addresult", "addresult");
            request.setAttribute("add", "add");
        }

        return mapping.findForward("libaddr-result");

    }

    /**
     * 修改通讯录
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward libaddredit(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request, HttpServletResponse response) {

        libaddr libaddr = (libaddr) getRecord(request, TABLE, KEY, ObjectName);

        request.setAttribute("department", ZhzSortMapUtil.getSortmap(libaddr.getDepartment(),
            baseDao));

        request.setAttribute("departnamelist", ZhzSortMapUtil.getChildSortMapList(departfatherid,
            baseDao));

        return mapping.findForward("libaddredit-form");

    }

    /**
     * 保存修改
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward libaddreditsave(ActionMapping mapping, ActionForm form,
                                         HttpServletRequest request, HttpServletResponse response) {
        String libaddrID = ZhzHttpRequestUtil.getParameters(request, KEY);

        //姓名
        String name = ZhzHttpRequestUtil.getParameters(request, "name");
        //部门
        String department = ZhzHttpRequestUtil.getParameters(request, "department");
        //职位
        String position = ZhzHttpRequestUtil.getParameters(request, "position");
        //电话
        String tel = ZhzHttpRequestUtil.getParameters(request, "tel");
        //email
        String email = ZhzHttpRequestUtil.getParameters(request, "email");
        //校区
        String area = ZhzHttpRequestUtil.getParameters(request, "area");
        //备注
        String memo = ZhzHttpRequestUtil.getParameters(request, "memo");

        libaddr la = (libaddr) baseDao.findOneObject(TABLE, KEY, libaddrID);

        la.setName(name);
        la.setDepartment(department);
        la.setPosition(position);
        la.setTel(tel);
        la.setEmail(email);
        la.setArea(area);
        la.setMemo(memo);
        la.setEditor(ZhzSecurityUtil.getOperatorName());
        la.setEditorTime(new Date());

        boolean update = baseDao.update(la);

        if (update) {
            request.setAttribute("editresult", "editresult");
            request.setAttribute("edit", "edit");
        }

        return mapping.findForward("libaddr-result");

    }

    /**
     * 删除通讯录
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward libaddrdel(ActionMapping mapping, ActionForm form,
                                    HttpServletRequest request, HttpServletResponse response) {
        delete(request, TABLE, KEY);

        return mapping.findForward("libaddr-result");
    }

    /**
     * 通过ajax请求，获取通讯录
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward libaddrContent(ActionMapping mapping, ActionForm form,
                                        HttpServletRequest request, HttpServletResponse response)
                                                                                                 throws Exception {
        libaddr libaddr = (libaddr) getRecord(request, TABLE, KEY, ObjectName);

        request.setAttribute("department", ZhzSortMapUtil.getSortmap(libaddr.getDepartment(),
            baseDao));

        return mapping.findForward("libaddr-content");
    }

    /**
     * 将libaddr转换为页面需要的数据
     *
     * @param news
     */
    private LibaddrVO changeToVo(libaddr libaddr) {
        LibaddrVO libaddrVO = new LibaddrVO();
        BeanUtils.copyProperties(libaddr, libaddrVO);

        Sortmap smp = (Sortmap) baseDao.findOneObject("Sortmap", "sortMapId", libaddr
            .getDepartment());
        if (smp != null)
            libaddrVO.setDepartmentString(smp.getValue());
        return libaddrVO;
    }

    public void setDepartfatherid(Integer departfatherid) {
        this.departfatherid = departfatherid;
    }
}